/*
 * Copyright 2017 Zhihu Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.zhihu.matisse.ui;

import android.app.Activity;
import android.content.Intent;
import android.content.res.TypedArray;
import android.database.Cursor;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;

import com.zhihu.matisse.R;
import com.zhihu.matisse.internal.entity.Album;
import com.zhihu.matisse.internal.entity.Item;
import com.zhihu.matisse.internal.entity.SelectionSpec;
import com.zhihu.matisse.internal.model.AlbumCollection;
import com.zhihu.matisse.internal.model.SelectedFileCollection;
import com.zhihu.matisse.internal.model.SelectedItemCollection;
import com.zhihu.matisse.internal.ui.BasePreviewActivity;
import com.zhihu.matisse.internal.ui.FilePreviewActivity;
import com.zhihu.matisse.internal.ui.FileSelectionFragment;
import com.zhihu.matisse.internal.ui.MediaPreviewActivity;
import com.zhihu.matisse.internal.ui.MediaSelectionFragment;
import com.zhihu.matisse.internal.ui.SelectedPreviewActivity;
import com.zhihu.matisse.internal.ui.adapter.FilesAdapter;
import com.zhihu.matisse.internal.ui.adapter.MediaAdapter;
import com.zhihu.matisse.internal.ui.adapter.SpinnerAdapter;
import com.zhihu.matisse.internal.ui.widget.AlbumsSpinner;
import com.zhihu.matisse.internal.ui.widget.CheckRadioView;
import com.zhihu.matisse.internal.ui.widget.IncapableDialog;
import com.zhihu.matisse.internal.utils.MediaStoreCompat;
import com.zhihu.matisse.internal.utils.PathUtils;
import com.zhihu.matisse.internal.utils.PhotoMetadataUtils;
import com.zhihu.matisse.internal.utils.SingleMediaScanner;

import java.io.File;
import java.util.ArrayList;

import static com.zhihu.matisse.internal.model.SelectedItemCollection.STATE_SELECTION;

/**
 * Main Activity to display albums and media content (images/videos) in each album
 * and also support media selecting operations.
 */
public class MatisseActivity extends AppCompatActivity implements
        AlbumCollection.AlbumCallbacks,
        MediaSelectionFragment.SelectionProvider, FileSelectionFragment.SelectionFileProvider,
        View.OnClickListener,
        FilesAdapter.CheckStateListener, FilesAdapter.OnMediaClickListener,
        FilesAdapter.OnPhotoCapture,
        MediaAdapter.CheckStateListener, MediaAdapter.OnMediaClickListener,
        MediaAdapter.OnPhotoCapture {

    public static final String EXTRA_RESULT_SELECTION_ITEM = "extra_result_selection_item";
    public static final String EXTRA_RESULT_SELECTION_FILE = "extra_result_selection_file";
    public static final String EXTRA_RESULT_SELECTION = "extra_result_selection";
    public static final String EXTRA_RESULT_SELECTION_PATH = "extra_result_selection_path";
    public static final String EXTRA_RESULT_ORIGINAL_ENABLE = "extra_result_original_enable";
    private static final int REQUEST_CODE_PREVIEW = 23;
    private static final int REQUEST_CODE_CAPTURE = 24;
    public static final String CHECK_STATE = "checkState";

    private SelectedItemCollection mSelectedCollection = new SelectedItemCollection(this);
    private SelectedFileCollection mSelectedFileCollection = new SelectedFileCollection(this);
    private SelectionSpec mSpec;

    private MediaStoreCompat mMediaStoreCompat;

    private final AlbumCollection mAlbumCollection = new AlbumCollection();
    private AlbumsSpinner mAlbumsSpinner;
    private SpinnerAdapter spinnerAdapter;

    private TextView mButtonPreview;
    private TextView mButtonApply;
    private View mContainer;
    private View mEmptyView;

    private LinearLayout mOriginalLayout;
    private CheckRadioView mOriginal;
    private boolean mOriginalEnable;
    private String defaultPath;

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        // programmatically set theme before super.onCreate()
        mSpec = SelectionSpec.getInstance();
        setTheme(mSpec.themeId);
        super.onCreate(savedInstanceState);
        if (!mSpec.hasInited) {
            setResult(RESULT_CANCELED);
            finish();
            return;
        }
        setContentView(R.layout.activity_matisse);

        if (mSpec.needOrientationRestriction()) {
            setRequestedOrientation(mSpec.orientation);
        }

        defaultPath = mSpec.defaultPath;//  Pictures/preventpro
        System.out.println("====onCreate=========="+defaultPath);

        if (mSpec.capture) {//设置是否可以拍照，并初始化数据
            mMediaStoreCompat = new MediaStoreCompat(this);
            if (mSpec.captureStrategy == null)
                throw new RuntimeException("Don't forget to set CaptureStrategy.");
            mMediaStoreCompat.setCaptureStrategy(mSpec.captureStrategy);
            mMediaStoreCompat.setDefaultPath(defaultPath);
        }

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayHomeAsUpEnabled(true);
        Drawable navigationIcon = toolbar.getNavigationIcon();
        TypedArray ta = getTheme().obtainStyledAttributes(new int[]{R.attr.album_element_color});
        int color = ta.getColor(0, 0);
        ta.recycle();
        navigationIcon.setColorFilter(color, PorterDuff.Mode.SRC_IN);

        mButtonPreview = (TextView) findViewById(R.id.button_preview);
        mButtonPreview.setOnClickListener(this);
        mButtonApply = (TextView) findViewById(R.id.button_apply);
        mButtonApply.setOnClickListener(this);
        mContainer = findViewById(R.id.container);
        mEmptyView = findViewById(R.id.empty_view);
        mOriginalLayout = findViewById(R.id.originalLayout);
        mOriginalLayout.setOnClickListener(this);
        mOriginal = findViewById(R.id.original);

        //已选中的图片
        if (mSpec.isUseMedia) {
            if (mSpec.selectItems != null){
                if(savedInstanceState == null){
                    savedInstanceState = new Bundle();
                }
                savedInstanceState.putParcelableArrayList(STATE_SELECTION, mSpec.selectItems);
            }
            mSelectedCollection.onCreate(savedInstanceState);
        } else {
            if (mSpec.selectFiles != null){
                if(savedInstanceState == null){
                    savedInstanceState = new Bundle();
                }
                savedInstanceState.putSerializable(STATE_SELECTION, mSpec.selectFiles);
            }
            mSelectedFileCollection.onCreate(savedInstanceState);
        }
        //是否使用原图
        if (savedInstanceState != null) {
            mOriginalEnable = savedInstanceState.getBoolean(CHECK_STATE);
        }
        //设置下发按钮状态
        updateBottomToolbar();

        initSpinner();

        if (mSpec.isUseMedia) {//公共目录下，使用cursor查图片
            mAlbumCollection.onCreate(this, this);
            mAlbumCollection.onRestoreInstanceState(savedInstanceState);
            mAlbumCollection.loadAlbums();//调用cursor查数据库
        } else {//私有目录下根据defaultPath直接找到图片
            File file = new File(defaultPath);
            onExternalFilesSelected(defaultPath);
        }
    }

    private void initSpinner() {
        TextView selectAlbum = (TextView) findViewById(R.id.selected_album);
        if(mSpec.isUseMedia){//公共目录时，才设置spinner
            selectAlbum.setEnabled(true);

            spinnerAdapter = new SpinnerAdapter(this, null, false);
            spinnerAdapter.setDefaultPath(defaultPath);
            mAlbumsSpinner = new AlbumsSpinner(this);
            mAlbumsSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    mAlbumCollection.setStateCurrentSelection(position);
                    spinnerAdapter.getCursor().moveToPosition(position);
                    Album album = Album.valueOf(spinnerAdapter.getCursor());
                    if (album.isAll() && SelectionSpec.getInstance().capture) {
                        album.addCaptureCount();
                    }
                    onAlbumSelected(album);
                }
                @Override
                public void onNothingSelected(AdapterView<?> parent) {}
            });
            if (!mSpec.choiceEnable) {//如果不能选择图集
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {//设置点击波浪，没啥用
                    selectAlbum.setForeground(null);
                }
                selectAlbum.setCompoundDrawables(null,null, null,null);
            }
            mAlbumsSpinner.setSelectedTextView(selectAlbum);
            mAlbumsSpinner.setChoiceEnable(mSpec.choiceEnable);
            mAlbumsSpinner.setAdapter(spinnerAdapter);
            mAlbumsSpinner.setDefaultPath(defaultPath);
            mAlbumsSpinner.setPopupAnchorView(findViewById(R.id.toolbar));//设置给toolbar
        }else {//私有目录的图片不需要添加adapter
            selectAlbum.setText("私有图片");
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {//设置点击波浪，没啥用
                selectAlbum.setForeground(null);
            }
            selectAlbum.setCompoundDrawables(null,null, null,null);
            selectAlbum.setEnabled(false);
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (mSpec.isUseMedia) {
            mSelectedCollection.onSaveInstanceState(outState);
            mAlbumCollection.onSaveInstanceState(outState);
        } else {
            mSelectedFileCollection.onSaveInstanceState(outState);
        }
        outState.putBoolean("checkState", mOriginalEnable);
    }

    @Override
    protected void onStart() {
        super.onStart();
        //设置立即拍照，跳转到系统相机界面
        if(mSpec.takePic){
            capture();
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mAlbumCollection.onDestroy();
        mSpec.onCheckedListener = null;
        mSpec.onSelectedListener = null;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        setResult(Activity.RESULT_CANCELED);
        super.onBackPressed();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_PREVIEW && resultCode == RESULT_OK) {
            Bundle resultBundle = data.getBundleExtra(BasePreviewActivity.EXTRA_RESULT_BUNDLE);
            if(mSpec.isUseMedia){
                ArrayList<Item> selected = resultBundle.getParcelableArrayList(STATE_SELECTION);
                int collectionType = resultBundle.getInt(SelectedItemCollection.STATE_COLLECTION_TYPE,
                        SelectedItemCollection.COLLECTION_UNDEFINED);
                mOriginalEnable = data.getBooleanExtra(BasePreviewActivity.EXTRA_RESULT_ORIGINAL_ENABLE, false);
                if (data.getBooleanExtra(BasePreviewActivity.EXTRA_RESULT_APPLY, false)) {
                    Intent result = new Intent();
                    ArrayList<Uri> selectedUris = new ArrayList<>();
                    ArrayList<String> selectedPaths = new ArrayList<>();
                    if (selected != null) {
                        for (Item item : selected) {
                            selectedUris.add(item.getContentUri());
                            selectedPaths.add(PathUtils.getPath(this, item.getContentUri()));
                        }
                    }
                    result.putParcelableArrayListExtra(EXTRA_RESULT_SELECTION_ITEM, selected);
                    result.putParcelableArrayListExtra(EXTRA_RESULT_SELECTION, selectedUris);
                    result.putStringArrayListExtra(EXTRA_RESULT_SELECTION_PATH, selectedPaths);
                    result.putExtra(EXTRA_RESULT_ORIGINAL_ENABLE, mOriginalEnable);
                    setResult(RESULT_OK, result);
                    finish();
                } else {
                    mSelectedCollection.overwrite(selected, collectionType);
                    Fragment mediaSelectionFragment = getSupportFragmentManager().findFragmentByTag(
                            MediaSelectionFragment.class.getSimpleName());
                    if (mediaSelectionFragment instanceof MediaSelectionFragment) {
                        ((MediaSelectionFragment) mediaSelectionFragment).refreshMediaGrid();
                    }
                    updateBottomToolbar();
                }
            }else {
                ArrayList<File> selected = (ArrayList<File>) resultBundle.getSerializable(STATE_SELECTION);
                int collectionType = resultBundle.getInt(SelectedItemCollection.STATE_COLLECTION_TYPE,
                        SelectedItemCollection.COLLECTION_UNDEFINED);
                mOriginalEnable = data.getBooleanExtra(BasePreviewActivity.EXTRA_RESULT_ORIGINAL_ENABLE, false);
                if (data.getBooleanExtra(BasePreviewActivity.EXTRA_RESULT_APPLY, false)) {
                    Intent result = new Intent();
                    ArrayList<String> selectedPaths = new ArrayList<>();
                    if (selected != null) {
                        for (File item : selected) {
                            selectedPaths.add(item.getAbsolutePath());
                        }
                    }
                    result.putExtra(EXTRA_RESULT_SELECTION_FILE, selected);
                    result.putStringArrayListExtra(EXTRA_RESULT_SELECTION_PATH, selectedPaths);
                    result.putExtra(EXTRA_RESULT_ORIGINAL_ENABLE, mOriginalEnable);
                    setResult(RESULT_OK, result);
                    finish();
                } else {
                    mSelectedFileCollection.overwrite(selected, collectionType);
                    Fragment mediaSelectionFragment = getSupportFragmentManager().findFragmentByTag(
                            FileSelectionFragment.class.getSimpleName());
                    if (mediaSelectionFragment instanceof FileSelectionFragment) {
                        ((FileSelectionFragment) mediaSelectionFragment).refreshMediaGrid();
                    }
                    updateBottomToolbar();
                }
            }

        } else if (requestCode == REQUEST_CODE_CAPTURE) {
            // Just pass the data back to previous calling Activity.
            if (resultCode == RESULT_OK) {
                Uri contentUri = mMediaStoreCompat.getCurrentPhotoUri();
                String path = mMediaStoreCompat.getCurrentPhotoPath();
                System.out.println("contentUri---->"+contentUri+"     path---->"+path);
                ArrayList<Uri> selected = new ArrayList<>();
                selected.add(contentUri);
                ArrayList<String> selectedPath = new ArrayList<>();
                selectedPath.add(path);
                Intent result = new Intent();
                result.putParcelableArrayListExtra(EXTRA_RESULT_SELECTION, selected);
                result.putStringArrayListExtra(EXTRA_RESULT_SELECTION_PATH, selectedPath);
                setResult(RESULT_OK, result);
                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
                    MatisseActivity.this.revokeUriPermission(contentUri,
                            Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);

                new SingleMediaScanner(this.getApplicationContext(), path, new SingleMediaScanner.ScanListener() {
                    @Override public void onScanFinish() {
                        Log.i("SingleMediaScanner", "scan finish!");
                    }
                });
            }
            finish();
        }
    }

    private void updateBottomToolbar() {
        int selectedCount = mSpec.isUseMedia? mSelectedCollection.count() : mSelectedFileCollection.count();
        if (selectedCount == 0) {
            mButtonPreview.setEnabled(false);
            mButtonApply.setEnabled(false);
            mButtonApply.setText(getString(R.string.button_apply_default));
        } else if (selectedCount == 1 && mSpec.singleSelectionModeEnabled()) {
            mButtonPreview.setEnabled(true);
            mButtonApply.setText(R.string.button_apply_default);
            mButtonApply.setEnabled(true);
        } else {
            mButtonPreview.setEnabled(true);
            mButtonApply.setEnabled(true);
            mButtonApply.setText(getString(R.string.button_apply, selectedCount));
        }


        if (mSpec.originalable) {
            mOriginalLayout.setVisibility(View.VISIBLE);
            updateOriginalState();
        } else {
            mOriginalLayout.setVisibility(View.INVISIBLE);
        }


    }


    private void updateOriginalState() {
        mOriginal.setChecked(mOriginalEnable);
        if (countOverMaxSize() > 0) {

            if (mOriginalEnable) {
                IncapableDialog incapableDialog = IncapableDialog.newInstance("",
                        getString(R.string.error_over_original_size, mSpec.originalMaxSize));
                incapableDialog.show(getSupportFragmentManager(),
                        IncapableDialog.class.getName());

                mOriginal.setChecked(false);
                mOriginalEnable = false;
            }
        }
    }


    private int countOverMaxSize() {
        int count = 0;
        int selectedCount = mSpec.isUseMedia? mSelectedCollection.count() : mSelectedFileCollection.count();
        for (int i = 0; i < selectedCount; i++) {
            if (mSpec.isUseMedia) {
                Item item = mSelectedCollection.asList().get(i);

                if (item.isImage()) {
                    float size = PhotoMetadataUtils.getSizeInMB(item.size);
                    if (size > mSpec.originalMaxSize) {
                        count++;
                    }
                }
            } else {
                File item = mSelectedFileCollection.asList().get(i);

                if (PathUtils.isImage(item)) {
                    float size = PhotoMetadataUtils.getSizeInMB(item.length());
                    if (size > mSpec.originalMaxSize) {
                        count++;
                    }
                }
            }
        }
        return count;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.button_preview) {
            Intent intent = new Intent(this, SelectedPreviewActivity.class);
            intent.putExtra(BasePreviewActivity.EXTRA_DEFAULT_BUNDLE,
                    mSpec.isUseMedia?mSelectedCollection.getDataWithBundle():mSelectedFileCollection.getDataWithBundle());
            intent.putExtra(BasePreviewActivity.EXTRA_RESULT_ORIGINAL_ENABLE, mOriginalEnable);
            startActivityForResult(intent, REQUEST_CODE_PREVIEW);
        } else if (v.getId() == R.id.button_apply) {
            Intent result = new Intent();
            if (mSpec.isUseMedia) {
                ArrayList<Uri> selectedUris = (ArrayList<Uri>) mSelectedCollection.asListOfUri();
                result.putParcelableArrayListExtra(EXTRA_RESULT_SELECTION, selectedUris);
                ArrayList<Item> selectedItems = (ArrayList<Item>) mSelectedCollection.asList();
                result.putParcelableArrayListExtra(EXTRA_RESULT_SELECTION_ITEM, selectedItems);
            } else {
                ArrayList<File> selectedItems = (ArrayList<File>) mSelectedFileCollection.asList();
                result.putExtra(EXTRA_RESULT_SELECTION_FILE, selectedItems);
            }
            ArrayList<String> selectedPaths = mSpec.isUseMedia?(ArrayList<String>) mSelectedCollection.asListOfString()
                    : (ArrayList<String>) mSelectedFileCollection.asListOfString();
            result.putStringArrayListExtra(EXTRA_RESULT_SELECTION_PATH, selectedPaths);
            result.putExtra(EXTRA_RESULT_ORIGINAL_ENABLE, mOriginalEnable);
            setResult(RESULT_OK, result);
            finish();
        } else if (v.getId() == R.id.originalLayout) {
            int count = countOverMaxSize();
            if (count > 0) {
                IncapableDialog incapableDialog = IncapableDialog.newInstance("",
                        getString(R.string.error_over_original_count, count, mSpec.originalMaxSize));
                incapableDialog.show(getSupportFragmentManager(),
                        IncapableDialog.class.getName());
                return;
            }

            mOriginalEnable = !mOriginalEnable;
            mOriginal.setChecked(mOriginalEnable);

            if (mSpec.onCheckedListener != null) {
                mSpec.onCheckedListener.onCheck(mOriginalEnable);
            }
        }
    }

    @Override
    public void onAlbumLoad(final Cursor cursor) {
        if (cursor.getCount() > 0) {
            for (int i = 0; i < cursor.getCount(); i++) {
                if (cursor.moveToPosition(i)) {
                    String bucket_display_name = cursor.getString(cursor.getColumnIndex("bucket_display_name"));
                    System.out.println("bucket_display_name========="+bucket_display_name);
                    //"preventpro"表示的是默认想要展示的文件夹的名字，也可以不设置就展示全部的
                    if (!TextUtils.isEmpty(bucket_display_name) && !TextUtils.isEmpty(defaultPath)
                            && defaultPath.contains(bucket_display_name)) {
                        mAlbumCollection.setStateCurrentSelection(i == 0 ? 1 : i);
                        break;
                    }
                }
            }
        }
        spinnerAdapter.swapCursor(cursor);
        // select default album.
        Handler handler = new Handler(Looper.getMainLooper());
        handler.post(new Runnable() {
            @Override
            public void run() {
                cursor.moveToPosition(mAlbumCollection.getCurrentSelection());
                mAlbumsSpinner.setSelection(MatisseActivity.this,
                        mAlbumCollection.getCurrentSelection());

                Album album = Album.valueOf(cursor);
                if (album.isAll() && SelectionSpec.getInstance().capture) {
                    album.addCaptureCount();
                }
                onAlbumSelected(album);
            }
        });
    }
    @Override
    public void onAlbumReset() {
        spinnerAdapter.swapCursor(null);
    }

    private void onAlbumSelected(Album album) {
        if (album.isAll() && album.isEmpty()) {
            mContainer.setVisibility(View.GONE);
            mEmptyView.setVisibility(View.VISIBLE);
        } else {
            mContainer.setVisibility(View.VISIBLE);
            mEmptyView.setVisibility(View.GONE);
            Fragment fragment = MediaSelectionFragment.newInstance(album);
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.container, fragment, MediaSelectionFragment.class.getSimpleName())
                    .commitAllowingStateLoss();
        }
    }

    private void onExternalFilesSelected(String path) {
        if (TextUtils.isEmpty(path)) {
            mContainer.setVisibility(View.GONE);
            mEmptyView.setVisibility(View.VISIBLE);
        } else {
            mContainer.setVisibility(View.VISIBLE);
            mEmptyView.setVisibility(View.GONE);
            Fragment fragment = FileSelectionFragment.newInstance(path);
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.container, fragment, FileSelectionFragment.class.getSimpleName())
                    .commitAllowingStateLoss();
        }
    }

    @Override
    public void onUpdate() {
        // notify bottom toolbar that check state changed.
        updateBottomToolbar();

        if (mSpec.onSelectedListener != null) {
            if (mSpec.isUseMedia) {
                mSpec.onSelectedListener.onSelected(
                        mSelectedCollection.asListOfUri(), mSelectedCollection.asListOfString());
            }else {
                mSpec.onSelectedListener.onSelected(
                        null, mSelectedFileCollection.asListOfString());
            }
        }
    }

    @Override
    public void onMediaClick(Album album, Item item, int adapterPosition) {
        Intent intent = new Intent(this, MediaPreviewActivity.class);
        intent.putExtra(MediaPreviewActivity.EXTRA_ALBUM, album);
        intent.putExtra(MediaPreviewActivity.EXTRA_ITEM, item);
        intent.putExtra(BasePreviewActivity.EXTRA_DEFAULT_BUNDLE, mSelectedCollection.getDataWithBundle());
        intent.putExtra(BasePreviewActivity.EXTRA_RESULT_ORIGINAL_ENABLE, mOriginalEnable);
        startActivityForResult(intent, REQUEST_CODE_PREVIEW);
    }

    @Override
    public void onMediaClick(File item, int adapterPosition) {
        Intent intent = new Intent(this, FilePreviewActivity.class);
        intent.putExtra(FilePreviewActivity.EXTRA_PATH, defaultPath);
        intent.putExtra(FilePreviewActivity.EXTRA_ITEM, item);
        intent.putExtra(BasePreviewActivity.EXTRA_DEFAULT_BUNDLE, mSelectedFileCollection.getDataWithBundle());
        intent.putExtra(BasePreviewActivity.EXTRA_RESULT_ORIGINAL_ENABLE, mOriginalEnable);
        startActivityForResult(intent, REQUEST_CODE_PREVIEW);
    }

    @Override
    public SelectedItemCollection provideSelectedItemCollection() {
        return mSelectedCollection;
    }

    @Override
    public void capture() {
        if (mMediaStoreCompat != null) {
            mMediaStoreCompat.dispatchCaptureIntent(this, REQUEST_CODE_CAPTURE);
        }
    }

    @Override
    public SelectedFileCollection provideSelectedFileCollection() {
        return mSelectedFileCollection;
    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }
}
